package com.fjec.market.rabbitmq.consumer;

import com.fjec.common.util.DateUtil;
import com.fjec.common.ys.protocol.ResRegister;
import com.fjec.common.ys.protocol.ResSearchReg;
import com.fjec.market.entity.BizYespayRegLog;
import com.fjec.market.entity.InfoYsepayRegister;
import com.fjec.market.service.IBizYespayRegLogService;
import com.fjec.market.service.IInfoYsepayRegisterService;
import com.mysql.cj.x.protobuf.MysqlxDatatypes;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.Map;

@Component
@Slf4j
public class YsRegisterResultQueryConsumer {

    @Resource
    private IInfoYsepayRegisterService infoYsepayRegisterService;

    @Resource
    private IBizYespayRegLogService bizYespayRegLogService;

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(
                            value = "${fjec.market.ysregisterresultquery.queue.name}",
                            durable = "${fjec.market.ysregisterresultquery.queue.durable}"
                    ),
                    exchange = @Exchange(
                            value = "${fjec.market.ysregisterresultquery.exchange.name}",
                            durable = "${fjec.market.ysregisterresultquery.exchange.durable}",
                            type = "${fjec.market.ysregisterresultquery.exchange.type}",
                            ignoreDeclarationExceptions = "${fjec.market.ysregisterresultquery.exchange.ignoreDeclarationExceptions}"
                    ),
                    key = "${fjec.market.ysregisterresultquery.router.key}"
            )
    )
    @RabbitHandler
    public void onYsRegisterResultQuery(Message message, Channel channel, @Headers Map<String, Object> headers) throws Exception {
        Long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        try {
            String strRegId = new String(message.getBody(), StandardCharsets.UTF_8);
            InfoYsepayRegister entity = infoYsepayRegisterService.getById(Long.valueOf(strRegId));
            ResSearchReg resSearchReg = infoYsepayRegisterService.searchYsRegByMerchantNo(entity);
            recordLog(entity, resSearchReg);
            channel.basicAck(deliveryTag, true);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            channel.basicReject(deliveryTag, false);
        }
    }

    private void recordLog(InfoYsepayRegister entity, ResSearchReg resSearchReg) {
        // 银盛进件log表
        BizYespayRegLog logEntity = new BizYespayRegLog();
        logEntity.setRegId(entity.getId());
        logEntity.setCustName(entity.getCustName());
        logEntity.setMerchantNo(entity.getMerchantNo1());
        logEntity.setCreateTime(DateUtil.nowLdt());
        logEntity.setOptType(2);
        if (resSearchReg != null) {
            logEntity.setReturnData(resSearchReg);
            if (resSearchReg.getCode() == 10000) {
                logEntity.setUserCode(resSearchReg.getUserCode());
                logEntity.setCustId(resSearchReg.getCustId());
                logEntity.setUserStatus(resSearchReg.getUserStatus());
                logEntity.setRegSuccess(1);
            } else {
                logEntity.setRegSuccess(0);
            }
        }
        bizYespayRegLogService.save(logEntity);
    }
}
